home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / ctutor2.zip / DOSEX.C < prev    next >
C/C++ Source or Header  |  1987-07-04  |  9KB  |  265 lines

  1. /******************************************************************/
  2. /* This is an example program to illustrate how to;               */
  3. /*  1. Get the time and date from DOS                             */
  4. /*  2. Set the cursor to any position on the screen               */
  5. /*  3. Read characters from the keyboard and display their codes  */
  6. /*  4. How to scroll a window up on the monitor                   */
  7. /*  5. Format a program for ease of reading and understanding     */
  8. /*  6. How to do proper prototyping                               */
  9. /******************************************************************/
  10.  
  11. void draw_box(void);
  12. void disp_char(int inchar);
  13. void get_time(int *hour,int *minute,int *second);
  14. void disp_time_date(void);
  15. void pos_cursor(char row,char column);
  16. void scroll_window(void);
  17.  
  18. #include "stdio.h"
  19. #include "dos.h"
  20. #include "conio.h"
  21.  
  22. int main()
  23. {
  24. int hour, minute, sec, old_sec;
  25. int character;
  26.  
  27.    draw_box();              /* draw the boxes around the fields */
  28.    old_sec = 0;             /* this variable stores the old time
  29.                                so we can look for a change      */
  30.  
  31.    do {
  32.       if (kbhit()) {                     /* has a key been hit? */
  33.          character = getch();            /* read it in          */
  34.          disp_char(character);           /* display it          */
  35.       }
  36.  
  37.       get_time(&hour,&minute,&sec);      /* get the time of day */
  38.       if (sec != old_sec) {              /* if it has changed,  */
  39.          disp_time_date();               /* update the display  */
  40.          old_sec = sec;                  /* save new time       */
  41.       }
  42.  
  43.    } while (character != 'Q');        /* Quit when a Q is found */
  44.  
  45.    pos_cursor(0,0);              /* put cursor at top of screen */
  46. }
  47.  
  48.  
  49. /* **************************************************** drawbox */
  50. /* This routine draws a box on the screen. The keys hit, and    */
  51. /* the time and date are displayed in these boxes. There is     */
  52. /* nothing special about these boxes, they are simply output    */
  53. /* using the printf function.                                   */
  54. /* ************************************************************ */
  55. void draw_box(void)
  56. {
  57. int index;
  58. char line[81];
  59.  
  60.    for (index = 0;index < 80;index++)       /* three blank rows */
  61.       line[index] = ' ';
  62.    line[80] = NULL;                            /* end of string */
  63.    for (index = 0;index < 3;index++)
  64.       printf("%s",line);
  65.  
  66.    line[8] = 201;                       /* draw top line of box */
  67.    for (index = 9;index < 70;index++)
  68.       line[index] = 205;
  69.    line[70] = 187;
  70.    printf("%s",line);
  71.  
  72.    line[8] = 186;                    /* draw sides of large box */
  73.    for (index = 9;index < 70;index++)
  74.       line[index] = ' ';
  75.    line[70] = 186;
  76.    for (index = 0;index < 15;index++)
  77.       printf("%s",line);
  78.  
  79.    line[8] = 204;                    /* draw line between boxes */
  80.    for (index = 9;index < 70;index++)
  81.       line[index] = 205;
  82.    line[70] = 185;
  83.    printf("%s",line);
  84.  
  85.    line[8] = 186;                    /* sides for time/date box */
  86.    for (index = 9;index < 70;index++)
  87.       line[index] = ' ';
  88.    line[70] = 186;
  89.    printf("%s",line);
  90.  
  91.    line[8] = 200;                     /* bottom line of the box */
  92.    for (index = 9;index < 70;index++)
  93.       line[index] = 205;
  94.    line[70] = 188;
  95.    printf("%s",line);
  96.  
  97.    for (index = 0;index < 80;index++)       /* three blank rows */
  98.       line[index] = ' ';
  99.    for (index = 0;index < 3;index++)
  100.       printf("%s",line);
  101.  
  102. }
  103.  
  104.  
  105. /* ************************************************** disp_char */
  106. /* This routine displays the characters hit on the monitor. If  */
  107. /* the first character is a zero, a special character has been  */
  108. /* hit, and the zero is displayed. The next character is read,  */
  109. /* and it is displayed on the monitor.                          */
  110. /* ************************************************************ */
  111. void disp_char(int inchar)
  112. {
  113.    scroll_window();
  114.    pos_cursor(17,15);          /* position of message on screen */
  115.  
  116.    if(inchar == 0) {
  117.       printf(" 00 ");            /* a special character was hit */
  118.       inchar = getch();          /* get the next part of it     */
  119.       switch (inchar) {
  120.          case 59  :
  121.          case 60  :
  122.          case 61  :
  123.          case 62  :
  124.          case 63  :              /* these are the function keys */
  125.          case 64  :
  126.          case 65  :
  127.          case 66  :
  128.          case 67  :
  129.          case 68  : printf("%4d Function key F%d\n",inchar,inchar-58);
  130.                     break;
  131.  
  132.          case 94  :
  133.          case 95  :
  134.          case 96  :
  135.          case 97  :
  136.          case 98  :         /* these are the ctrl-function keys */
  137.          case 99  :
  138.          case 100 :
  139.          case 101 :
  140.          case 102 :
  141.          case 103 : printf("%4d Function key Ctrl-F%d\n",inchar,
  142.                        inchar-93);
  143.                     break;
  144.  
  145.          case 84  :
  146.          case 85  :
  147.          case 86  :
  148.          case 87  :        /* these are the upper-function keys */
  149.          case 88  :
  150.          case 89  :
  151.          case 90  :
  152.          case 91  :
  153.          case 92  :
  154.          case 93  : printf("%4d Function key Upper-F%d\n",inchar,
  155.                        inchar-83);
  156.                     break;
  157.  
  158.          case 104 :
  159.          case 105 :
  160.          case 106 :
  161.          case 107 :
  162.          case 108 :          /* these are the alt-function keys */
  163.          case 109 :
  164.          case 110 :
  165.          case 111 :
  166.          case 112 :
  167.          case 113 : printf("%4d Function key Alt-F%d\n",inchar,
  168.                        inchar-103);
  169.                     break;
  170.  
  171.          default  : printf("%4d Special key hit\n",inchar);
  172.       }
  173.  
  174.    } else                        /* a regular character was hit */
  175.       printf("    %4d (%c) Character Hit.\n",inchar,inchar);
  176.  
  177.    pos_cursor(25,1);        /* hide the cursor on the 26th line */
  178. }
  179.  
  180.  
  181. /* *************************************************** get_time */
  182. /* This routine calls the DOS function call for time of day. It */
  183. /* returns the time of day to the calling program in the three  */
  184. /* pointers used in the call.                                   */
  185. /* ************************************************************ */
  186. void get_time(int *hour,int *minute,int *second)
  187. {
  188. union REGS inregs;
  189. union REGS outregs;
  190.  
  191.    inregs.h.ah = 44;               /* Hex 2C - Get current time */
  192.    int86(0x21,&inregs,&outregs);
  193.    *hour = outregs.h.ch;
  194.    *minute = outregs.h.cl;
  195.    *second = outregs.h.dh;
  196. }
  197.  
  198.  
  199. /* ********************************************* disp_time_date */
  200. /* This routine displays the time and date on the monitor in a  */
  201. /* fixed position. It gets the time from the get_time function, */
  202. /* and gets the date from its own built in DOS call. Good       */
  203. /* programming practice would move the date to another function */
  204. /* but this is an illustrative example to display methods of    */
  205. /* doing things. This routine also calls the cursor positioning */
  206. /* function to put the time and date where we want them.        */
  207. /* ************************************************************ */
  208. void disp_time_date(void)
  209. {
  210. int hour, minute, second;
  211. union REGS inregs;
  212. union REGS outregs;
  213.  
  214.    pos_cursor(19,19);  /* position the cursor for date and time */
  215.  
  216.    inregs.h.ah = 42;              /* hex 2A - What is the date? */
  217.    int86(0x21,&inregs,&outregs);  /* interrupt 21               */
  218.    printf("Date = %2d/%2d/%2d    ",
  219.       outregs.h.dh,                 /* month - 1 to 12          */
  220.       outregs.h.dl,                 /* day - 1 to 31            */
  221.       outregs.x.cx);                /* year - 1980 to 2099      */
  222.  
  223.    get_time(&hour, &minute, &second);
  224.    printf("Time = %2d:%2d:%2d\n",hour, minute, second);
  225.  
  226.    pos_cursor(25,1);        /* hide the cursor on the 26th line */
  227. }
  228.  
  229. /* ************************************************* pos_cursor */
  230. /* This routine positions the cursor at the requested row and   */
  231. /* column. The upper left corner is row 0 and column 0          */
  232. /* ************************************************************ */
  233. void pos_cursor(char row,char column)
  234. {
  235. union REGS inregs;
  236. union REGS outregs;
  237.  
  238.    inregs.h.ah = 2;        /* service 2 - position the cursor   */
  239.    inregs.h.dh = row;
  240.    inregs.h.dl = column;
  241.    inregs.h.bh = 0;
  242.    int86(0x10,&inregs,&outregs);                /* interrupt 10 */
  243. }
  244.  
  245.  
  246. /* ********************************************** scroll_window */
  247. /* This routine scrolls all of the material in the key hit      */
  248. /* window up one space leaving room for another entry.          */
  249. /* ************************************************************ */
  250. void scroll_window(void)
  251. {
  252. union REGS inregs;
  253. union REGS outregs;
  254.  
  255.    inregs.h.ah = 6;      /* service 6 - scroll window           */
  256.    inregs.h.al = 1;      /* number of lines to scroll           */
  257.    inregs.h.ch = 3;      /* top row of window                   */
  258.    inregs.h.cl = 9;      /* left column of window               */
  259.    inregs.h.dh = 17;     /* bottom row of window                */
  260.    inregs.h.dl = 69;     /* right column of window              */
  261.    inregs.h.bh = 7;      /* attribute of blank line             */
  262.    int86(0x10,&inregs,&outregs);                /* interrupt 10 */
  263.  
  264. }
  265.